<?php


namespace app\api\controller\Index;


use app\admin\model\Admin\MessageModel;
use app\admin\model\Video\VideoChapterModel;
use app\api\controller\BaseController;
use app\api\model\Banner\BannerModel;
use app\api\model\Course\CourseModel;
use app\api\model\Order\OrderModel;
use app\api\model\School\SchoolModel;
use app\api\model\Video\VideoModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class Index extends BaseController
{
    /**
     * @api {get} /get_index 首页数据
     * @apiGroup 首页
     * @apiSuccess {int} code 状态码 0：正常 -1：错误
     * @apiSuccess {json} data 数据源
     * @apiSuccess {string} msg 描述
     * @apiVersion 1.0.0
     */
    public function index(Request $request)
    {
        $data = [];
        //首页Banner
        $data['banner_list'] = BannerModel::getBannerWhereMore([['type_id','=',1],['is_show','=',1]],"id,title,type_id,img,link_url");
        foreach ($data['banner_list'] as &$banner){
            $banner['img'] = ImageUtil::i()->replaceUrl($banner['img']);
        }
        //首页广告
        $data['advert_list'] = BannerModel::getBannerWhereMore([['type_id','=',2],['is_show','=',1]],"id,title,type_id,img,link_url");
        foreach ($data['advert_list'] as &$banner){
            $banner['img'] = ImageUtil::i()->replaceUrl($banner['img']);
        }
        //首页滚动通知
        $data['message_list'] = MessageModel::getMessageWhereMore([],"id,title,details,author,is_top");
        foreach ($data['message_list'] as &$message){
            $message['details'] = htmlspecialchars_decode($message['details']);
        }
        //推荐课程
        $data['course_list'] = CourseModel::getCourseWhereMore([['c.is_recommend','=',1],['c.is_show','=',1],['c.is_del','=',0]],'c.id,c.title,c.img,c.brief,c.price,c.vip_price,c.cost_price,c.school_id,c.is_group,c.group_num,c.start_time,c.end_time,cc.cate_name');
        foreach ($data['course_list'] as &$course){
            $course['img'] = ImageUtil::i()->replaceUrl($course['img']);
            /*$images = !empty($course['slider_img']) ? json_decode($course['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $course['slider_img'] = $images;
            $course['details'] = !empty($course['details']) ? htmlspecialchars_decode($course['details']) : "";*/
            //总报名人数
            $or_where = [
                ['o.type', '=', 3],
                ['og.type', '=', 3],
                ['o.status', '<>', 4],
                ['o.status', '<>', -1],
                ['og.goods_id', '=', $course['id']],
                ['o.add_time', '>=', $course['start_time']],
                ['o.add_time', '<=', $course['end_time']],
            ];
            $course['course_all_count'] = count(OrderModel::getOrderWhereJoinMore($or_where));
        }
        //推荐视频
        $data['video_list'] = VideoModel::getVideoWhereMore([['is_recommend','=',1],['is_show','=',1]],'id,video_title,img,label,play_num,play_default,school_id');
        foreach ($data['video_list'] as &$video){
            $video['label'] = !empty($video['label']) ? json_decode($video['label'],true) : [];
            $video['img'] = ImageUtil::i()->replaceUrl($video['img']);
            $chapters = VideoChapterModel::getVideoChapterWhereMore([['video_id','=',$video['id']]],"is_free");
            $is_free = 0;
            foreach ($chapters as $chapter){
                if(!empty($chapter['is_free'])){
                    $is_free = 1;
                    break;
                }else{
                    continue;
                }
            }
            $video['is_free'] = $is_free;
        }
        //推荐学校
        $data['school_list'] = SchoolModel::getSchoolWhereMore([['is_recommend','=',1],['is_show','=',1]],'id,school_name,brief,img,area_id');
        foreach ($data['school_list'] as &$school){
            $school['img'] = ImageUtil::i()->replaceUrl($school['img']);
            /*$images = !empty($school['slider_img']) ? json_decode($school['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $school['slider_img'] = $images;
            $school['details'] = !empty($school['details']) ? htmlspecialchars_decode($school['details']) : "";*/
        }
        return Util::returnJson(self::CODE_SUCCESS,$data);
    }

}